From 6c20085429304a5cd3362a15d2f75755d49566a3 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Thu, 23 Oct 2014 08:30:36 -0400 Subject: [PATCH] cargo new: Don't create a git repo in subdirectory Running `cargo new` to create a subpackage shouldn't create another git repository if one exists in a parent directory. Closes #664 --- Cargo.lock | 8 ++++---- src/cargo/ops/cargo_new.rs | 8 +++++++- src/cargo/util/vcs.rs | 3 +++ tests/test_cargo_new.rs | 35 ++++++++++++++++++++++++++--------- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4dd9d66df..df26dcd62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,7 +5,7 @@ dependencies = [ "curl 0.0.1 (git+https://github.com/alexcrichton/curl-rust?ref=bundle#36b015de91daf6310227cec04ef30acf5929dfb6)", "docopt 0.6.4 (git+https://github.com/docopt/docopt.rs#4544a9f422b115c2ffef4ee9baf27ceb07c34602)", "flate2 0.0.1 (git+https://github.com/alexcrichton/flate2-rs#68971ae77a523c7ec3f19b4bcd195f76291ea390)", - "git2 0.0.1 (git+https://github.com/alexcrichton/git2-rs#c01b0b279470552c48cf7f902ae5baf132df0a6d)", + "git2 0.0.1 (git+https://github.com/alexcrichton/git2-rs#7d7fba10893590793ae88c8fc6ab2aeffcb8f10b)", "glob 0.0.1 (git+https://github.com/rust-lang/glob#469a6bc1a0fc289ab220170e691cffbc01dcf1e6)", "hamcrest 0.1.0 (git+https://github.com/carllerche/hamcrest-rust.git#7d46e76514ac606530dfb0e93270fffcf64ca76d)", "semver 0.1.0 (git+https://github.com/rust-lang/semver#9bb8265ea6cf01eddfa7dc5ec9334883443e9fc7)", @@ -47,9 +47,9 @@ source = "git+https://github.com/alexcrichton/flate2-rs#68971ae77a523c7ec3f19b4b [[package]] name = "git2" version = "0.0.1" -source = "git+https://github.com/alexcrichton/git2-rs#c01b0b279470552c48cf7f902ae5baf132df0a6d" +source = "git+https://github.com/alexcrichton/git2-rs#7d7fba10893590793ae88c8fc6ab2aeffcb8f10b" dependencies = [ - "libgit2 0.0.1 (git+https://github.com/alexcrichton/git2-rs#c01b0b279470552c48cf7f902ae5baf132df0a6d)", + "libgit2 0.0.1 (git+https://github.com/alexcrichton/git2-rs#7d7fba10893590793ae88c8fc6ab2aeffcb8f10b)", "url 0.1.0 (git+https://github.com/servo/rust-url#7f1991d847fb8cf8648def2ff121bae90b89131f)", ] @@ -66,7 +66,7 @@ source = "git+https://github.com/carllerche/hamcrest-rust.git#7d46e76514ac606530 [[package]] name = "libgit2" version = "0.0.1" -source = "git+https://github.com/alexcrichton/git2-rs#c01b0b279470552c48cf7f902ae5baf132df0a6d" +source = "git+https://github.com/alexcrichton/git2-rs#7d7fba10893590793ae88c8fc6ab2aeffcb8f10b" dependencies = [ "libssh2-static-sys 0.0.1 (git+https://github.com/alexcrichton/libssh2-static-sys#80e71a3021618eb05656c58fb7c5ef5f12bc747f)", "link-config 0.0.1 (git+https://github.com/alexcrichton/link-config#0202cc8aa74a7b0bdbaef4eef368d0bc80f63691)", diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 856603bfe..d5c0d18ef 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -41,9 +41,15 @@ pub fn new(opts: NewOptions, _shell: &mut MultiShell) -> CargoResult<()> { }) } +fn existing_git_repo(path: &Path) -> bool { + GitRepo::discover(path).is_ok() +} + fn mk(path: &Path, name: &str, opts: &NewOptions) -> CargoResult<()> { let cfg = try!(global_config()); let mut ignore = "/target\n".to_string(); + let no_git = !opts.git && (opts.no_git || cfg.git == Some(false)); + let in_existing_git_repo = existing_git_repo(&path.dir_path()); if !opts.bin { ignore.push_str("/Cargo.lock\n"); } @@ -51,7 +57,7 @@ fn mk(path: &Path, name: &str, opts: &NewOptions) -> CargoResult<()> { if opts.hg { try!(HgRepo::init(path)); try!(File::create(&path.join(".hgignore")).write(ignore.as_bytes())); - } else if !opts.git && (opts.no_git || cfg.git == Some(false)) { + } else if no_git || in_existing_git_repo { try!(fs::mkdir(path, io::USER_RWX)); } else { try!(GitRepo::init(path)); diff --git a/src/cargo/util/vcs.rs b/src/cargo/util/vcs.rs index 04b2fb376..b4f7ce290 100644 --- a/src/cargo/util/vcs.rs +++ b/src/cargo/util/vcs.rs @@ -10,6 +10,9 @@ impl GitRepo { try!(git2::Repository::init(path)); return Ok(GitRepo) } + pub fn discover(path: &Path) -> Result { + git2::Repository::discover(path) + } } impl HgRepo { diff --git a/tests/test_cargo_new.rs b/tests/test_cargo_new.rs index cc2184838..7539a0475 100644 --- a/tests/test_cargo_new.rs +++ b/tests/test_cargo_new.rs @@ -53,17 +53,18 @@ test!(simple_bin { }) test!(simple_git { + let td = TempDir::new("cargo").unwrap(); os::setenv("USER", "foo"); - assert_that(cargo_process("new").arg("foo"), + assert_that(cargo_process("new").arg("foo").cwd(td.path().clone()), execs().with_status(0)); - assert_that(&paths::root().join("foo"), existing_dir()); - assert_that(&paths::root().join("foo/Cargo.toml"), existing_file()); - assert_that(&paths::root().join("foo/src/lib.rs"), existing_file()); - assert_that(&paths::root().join("foo/.git"), existing_dir()); - assert_that(&paths::root().join("foo/.gitignore"), existing_file()); + assert_that(td.path(), existing_dir()); + assert_that(&td.path().join("foo/Cargo.toml"), existing_file()); + assert_that(&td.path().join("foo/src/lib.rs"), existing_file()); + assert_that(&td.path().join("foo/.git"), existing_dir()); + assert_that(&td.path().join("foo/.gitignore"), existing_file()); - assert_that(cargo_process("build").cwd(paths::root().join("foo")), + assert_that(cargo_process("build").cwd(td.path().clone().join("foo")), execs().with_status(0)); }) @@ -157,6 +158,7 @@ test!(author_prefers_cargo { test!(git_prefers_command_line { let root = paths::root(); + let td = TempDir::new("cargo").unwrap(); fs::mkdir(&root.join(".cargo"), USER_RWX).assert(); File::create(&root.join(".cargo/config")).write_str(r#" [cargo-new] @@ -165,8 +167,23 @@ test!(git_prefers_command_line { email = "bar" "#).assert(); - assert_that(cargo_process("new").arg("foo").arg("--git") + assert_that(cargo_process("new").arg("foo").arg("--git").cwd(td.path().clone()) .env("USER", Some("foo")), execs().with_status(0)); - assert!(root.join("foo/.gitignore").exists()); + assert!(td.path().join("foo/.gitignore").exists()); +}) + +test!(subpackage_no_git { + os::setenv("USER", "foo"); + assert_that(cargo_process("new").arg("foo"), execs().with_status(0)); + + let subpackage = paths::root().join("foo").join("components"); + fs::mkdir(&subpackage, USER_RWX).assert(); + assert_that(cargo_process("new").arg("foo/components/subcomponent"), + execs().with_status(0)); + + assert_that(&paths::root().join("foo/components/subcomponent/.git"), + is_not(existing_file())); + assert_that(&paths::root().join("foo/components/subcomponent/.gitignore"), + is_not(existing_file())); }) -- 2.30.2